package com.ygj.meeting.action;

import static com.base.common.GlobaConstants.ACTION_RESULT_DATA;
import static com.base.common.GlobaConstants.ASYNCHRONOUS;
import static com.base.common.GlobaConstants.CONTENT_TYPE;
import static com.base.common.GlobaConstants.CONTENT_TYPE_JSON;
import static com.base.common.GlobaConstants.JSON;
import static com.base.common.GlobaConstants.ROOT;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.base.common.action.AbstractBaseAction;
import com.base.common.bean.ActionResult;
import com.base.common.enums.MeetingStatus;
import com.base.common.persistent.model.Employee;
import com.base.common.persistent.model.Meeting;
import com.base.common.persistent.model.MeetingSignRecord;
import com.base.common.persistent.model.SignPoint;
import com.cjy.common.bean.OpAttributes;
import com.ygj.meeting.service.MeetingService;

/**
 * 会议签到action
 * 
 * @author Kat Colin
 *
 */

@Action("meetingAction")
@Results({ @Result(name = ASYNCHRONOUS, type = JSON, params = { ROOT,
		ACTION_RESULT_DATA, CONTENT_TYPE, CONTENT_TYPE_JSON }) })
public class MeetingAction extends AbstractBaseAction {

	private static final long serialVersionUID = 1L;

	@Resource
	private MeetingService meetingService;

	// 获取签到点参数
	private int compoundsId; // 当前小区id

	// 增加会议参数
	private String name; // 会议名称
	private String address; // 会议地址
	private String description; // 会议描述
	private Date startTime; // 会议开始时间
	private Date endTime; // 会议结束时间
	private int isNeedAuthorize; // 是否需要授权 默认为0，不需要授权，1则是需要授权
	private int signPointId; // 签到点Id
	private String employeeIdListStr; // 指定参加会议员工id的List JOSN字符串
	
	// 会议签到参数
	private int meetingId;
	
	// 查询参数
	private int page;
	private int status;

	public int getSignPointId() {
		return signPointId;
	}

	public void setSignPointId(int signPointId) {
		this.signPointId = signPointId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public int getCompoundsId() {
		return compoundsId;
	}

	public void setCompoundsId(int compoundsId) {
		this.compoundsId = compoundsId;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getEmployeeIdListStr() {
		return employeeIdListStr;
	}

	public void setEmployeeIdListStr(String employeeIdListStr) {
		this.employeeIdListStr = employeeIdListStr;
	}

	public int getMeetingId() {
		return meetingId;
	}

	public void setMeetingId(int meetingId) {
		this.meetingId = meetingId;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public int getIsNeedAuthorize() {
		return isNeedAuthorize;
	}

	public void setIsNeedAuthorize(int isNeedAuthorize) {
		this.isNeedAuthorize = isNeedAuthorize;
	}

	/**
	 * 根据小区查询签到点
	 * 
	 * @return
	 */
	public String getSignPointByCompounds() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add("result",
					meetingService.getSignPointByCompounds(compoundsId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 添加会议
	 * 
	 * @return
	 */
	public String addMeeting() {
		try {
			Meeting meeting = new Meeting();
			meeting.setDescription(description);
			meeting.setIsNeedAuthorize(isNeedAuthorize);
			meeting.setStartTime(startTime);
			meeting.setEndTime(endTime);
			meeting.setName(name);
			meeting.setAddress(address);
			meeting.setStatus(MeetingStatus.NOTBEGINNING.getValue());
			meeting.setCreateEmployee(new Employee()
					.setId(((List<Employee>) request.getSession().getAttribute(
							"users")).get(0).getId()));
			SignPoint signPoint = new SignPoint();
			signPoint.setId(signPointId);
			meeting.setSignPoint(signPoint);
			int result = meetingService.addMeeting(meeting,
					isNeedAuthorize == 1 ? employeeIdListStr : null);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 根据签到点查询正在召开或者未召开的会议
	 * 
	 * @return
	 */
	public String queryCouldJoinMeetingBySignPoint() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 1).add(
					"result",
					meetingService
							.queryCouldJoinMeetingBySignPoint(signPointId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 会议签到
	 * 
	 * @return
	 */
	public String sign() {
		try {
			int code = 0;
			MeetingSignRecord meetingSignRecord = new MeetingSignRecord();
			Meeting meeting = new Meeting();
			meeting.setId(meetingId);
			meetingSignRecord.setMeeting(meeting);
			meetingSignRecord.setSignInTime(new Date());
			meetingSignRecord.setEmployee(new Employee().setId(((List<Employee>)request.getSession().getAttribute("users")).get(0).getId()));
			meetingSignRecord.setAddress(address);
			
			code = meetingService.sign(meetingSignRecord);
			/*if(code == 1) {
				if (meetingId == 31) {
					List<AnnualMeetingRecord> annualMeetingRecords = new ArrayList<AnnualMeetingRecord>();

					AnnualMeetingRecord annualMeetingRecord = new AnnualMeetingRecord();
					Employee employee = ((List<Employee>)request.getSession().getAttribute("users")).get(0);
					annualMeetingRecord.setEmployeeName(employee.getName());
					annualMeetingRecord.setUrl(employee.getHeadPicUrl());
					annualMeetingRecord.setEmployeeId(employee.getId());
					annualMeetingRecords.add(annualMeetingRecord);
					
					request.getServletContext().setAttribute("UnShowMeeingSignRecord", annualMeetingRecords);
				}
				
			}*/
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", code));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	/**
	 * 判断是否已经签到
	 * 
	 * @return
	 */
	public String isHaveSign(){
		try {
			int result = meetingService.isHaveSign(((List<Employee>)request.getSession().getAttribute("users")).get(0).getId(), meetingId);
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", result == 0 ? 1 : 2));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	/**
	 * 模糊查询会议
	 * 
	 * @return
	 */
	public String queryMeetingsByCondition() {
		try {
			Map<String, Object> conditions = new HashMap<String, Object>();
			if (name != null) {
				conditions.put("name", name.trim());
			}
			conditions.put("signPointId", signPointId);
			conditions.put("startTime", startTime);
			conditions.put("endTime", endTime);
			conditions.put("compoundsId", compoundsId);
			conditions.put("status", status);
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", meetingService.queryMeetingsByCondition(conditions, page)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	/**
	 * 模糊查询会议次数
	 * @author Kat Colin
	 * @return
	 */
	public String queryMeetingCountsByCondition(){
		try {
			Map<String, Object> conditions = new HashMap<String, Object>();
			
			if (name != null) {conditions.put("name", name.trim());};
			conditions.put("signPointId", signPointId);
			conditions.put("startTime", startTime);
			conditions.put("endTime", endTime);
			conditions.put("compoundsId", compoundsId);
			conditions.put("status", status);
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", new OpAttributes().add("totalCounts", meetingService.queryMeetingCountsByCondition(conditions))));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}

	/**
	 * 根据会议id获取会议签到记录
	 * @author Kat Colin
	 * @return
	 */
	public String getMeetingSignRecordByMeetingId() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes().add("code", 1).add("result", meetingService.queryMeetingRecordByMeetingId(meetingId)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes().add(
					"code", 0));
		}
		return returnType();
	}
	
	/**
	 * 获取会议授权员工
	 * 
	 * @return
	 */
	public String getJoinEmployee() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 1).add("result", meetingService.queryJoinEmployee(meetingId)));
		} catch (Exception e) {
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
		}
		return returnType();
	}
	
	public String updateJoinEmployee() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", meetingService.updateJoinEmployee(meetingId, employeeIdListStr)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
		}
		
		return returnType();
	}
	
	public String updateMeeting() {
		try {
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", meetingService.updateMeeting(meetingId, name, description, startTime, endTime)));
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes<>().add("code", 0));
		}
		return returnType();
	}
}
